package vu.alfonsodiecko.stundenplanfhjena.gui;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.holoeverywhere.widget.ProgressBar;

import vu.alfonsodiecko.stundenplanfhjena.DateHelper;
import vu.alfonsodiecko.stundenplanfhjena.OnlineHelper;
import vu.alfonsodiecko.stundenplanfhjena.Project;
import vu.alfonsodiecko.stundenplanfhjena.R;
import vu.alfonsodiecko.stundenplanfhjena.datentyp.bus.Haltestelle;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;

public class BusLoader extends ANotifyingScreenLoading {

	private ProgressBar progress;
	private TextView text;
	private List<Haltestelle> halteStelleListe = null;
	private String haltestelle = null;
	private LinearLayout busPlanLayout;
	private Context con;
	private List<TextView> listText = null;
	private int time;
	private int state =1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loader1);
		 progress = (ProgressBar) findViewById(R.id.progressBarLoad);
		 text = (TextView) findViewById(R.id.textViewLoader);
		busPlanLayout = (LinearLayout) findViewById(R.id.busPlanLayout);
		Bundle b = getIntent().getExtras();
		haltestelle = b.getString("key");
		time = b.getInt("time");
		ActionBar navigation_bar = getSupportActionBar();
		// Setting standart navigation bar view
		navigation_bar.setTitle("Haltestelle");
		navigation_bar.setSubtitle(haltestelle);
		con = this.getApplicationContext();
		startProgress();

	}

	// protected void onPause() {
	// mHandler.
	// }
	private void createMainWindow() {
		listText = new ArrayList<TextView>();
		busPlanLayout.removeAllViews();
		if (halteStelleListe != null) {
			int i = 0;
			for (Haltestelle stop : halteStelleListe) {
				if (i == 0) {
					String source = "Haltestelle:" + stop.getHaltestelle();
					TextView text = new TextView(con);
					text.setText(source);
					text.setTextColor(this.getResources().getColor(R.color.fh_green));
					text.setTextSize(20);
					text.setPadding(10, 0, 0, 10);
					busPlanLayout.addView(text);
					i++;
				}
				Calendar c = Calendar.getInstance();
				String source = "Linie: " + stop.getLinie() + " nach " + stop.getZiel();
				String source2 = "Ankunftszeit: " + DateHelper.getTimeWitheSecondsString(stop.getAkunftszeit()) + " in ";
				String source3 = getTimeDiff(stop.getAkunftszeit().getTime(), c.getTimeInMillis());
				TextView text = new TextView(con);
				text.setText(source);
				text.setTextSize(14);
				text.setTextColor(this.getResources().getColor(R.color.fh_green_light));
				text.setPadding(10, 0, 0, 10);
				TextView text2 = new TextView(con);
				text2.setText(source2);
				text2.setTextSize(12);
				text2.setPadding(10, 0, 0, 10);
				text2.setTextColor(this.getResources().getColor(R.color.Black2));
				TextView text3 = new TextView(con);
				text3.setText(source3);
				text3.setTextSize(12);
				text3.setPadding(10, 0, 0, 10);
				text3.setTextColor(this.getResources().getColor(R.color.RedDark));
				View vie = new View(con);
				vie.setBackgroundColor(this.getResources().getColor(R.color.fh_green));
				vie.setMinimumHeight(3);
				vie.setMinimumWidth(getWindowManager().getDefaultDisplay().getWidth());
				busPlanLayout.addView(text);
				LinearLayout lay = new LinearLayout(con);
				lay.setOrientation(LinearLayout.HORIZONTAL);
				lay.addView(text2);
				lay.addView(text3);
				listText.add(text2);
				busPlanLayout.addView(lay);
				busPlanLayout.addView(vie);
			}
		}
	}

	private String getTimeDiff(long time, long timeInMillis) {
		long diff = time - timeInMillis;
		if (diff < 0) {
			return "verpasst";
		}
		long diffSeconds = diff / 1000 % 60;
		long diffMinutes = diff / (60 * 1000) % 60;
		long diffHours = diff / (60 * 60 * 1000);
		String zeit = fixValue(String.valueOf(diffHours)) + "h " + fixValue(String.valueOf(diffMinutes)) + "min "
				+ fixValue(String.valueOf(diffSeconds)) + "s";
		return zeit;
	}

	private String fixValue(String valueOf) {
		if (valueOf.length() == 0) {
			return "00";
		}
		if (valueOf.length() == 1) {
			return "0" + valueOf;
		}
		return valueOf;
	}

	@Override
	protected boolean doInbackground() {
		halteStelleListe = OnlineHelper.getStops(haltestelle, Project.Websites.busStops.replace("vsz=120", "vsz=" + String.valueOf(time)));
		return true;
	}

	@Override
	protected boolean reloadGUI() {
		runOnUiThread(new Runnable() {
			public void run() {
				createMainWindow();
			}
		});

		return false;
	}
	@Override
	protected boolean setProgress() {
		return progress.post(new Runnable() {
			@Override
			public void run() {
				text.setText("Loading " + String.valueOf(state) + "s");
				progress.setProgress(state);
				state++;
			}
		});
	}

}
